<template>
    <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
                class="-mb-15px"
                :model="queryParams"
                ref="queryFormRef"
                :inline="true"
                label-width="68px"
        >
            <el-form-item label="租户ID" prop="rechargeTenantId">
                <el-select
                        v-model="queryParams.rechargeTenantId"
                        placeholder="请选择租户ID"
                        clearable
                        class="!w-240px"
                >
                    <el-option label="请选择字典生成" value="" />
                </el-select>
            </el-form-item>

            <el-form-item label="密钥ID" prop="secretId">
                <el-input
                        v-model="queryParams.secretId"
                        placeholder="请输入对应的密钥ID"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                />
            </el-form-item>

            <el-form-item label="充值金额" prop="rechargeAmount">
                <el-input
                        v-model="queryParams.rechargeAmount"
                        placeholder="请输入充值金额"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                />
            </el-form-item>

            <el-form-item label="充前余额" prop="beforeBalance">
                <el-input
                        v-model="queryParams.beforeBalance"
                        placeholder="请输入充值前余额"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                />
            </el-form-item>

            <el-form-item label="充后余额" prop="afterBalance">
                <el-input
                        v-model="queryParams.afterBalance"
                        placeholder="请输入充值后余额"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                />
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input
                        v-model="queryParams.remark"
                        placeholder="请输入备注"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                />
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime">
                <el-date-picker
                        v-model="queryParams.createTime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        type="daterange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                        class="!w-220px"
                />
            </el-form-item>

            <el-form-item>
                <el-button @click="handleQuery">
                    <Icon icon="ep:search" class="mr-5px" /> 搜索
                </el-button>
                <el-button @click="resetQuery">
                    <Icon icon="ep:refresh" class="mr-5px" /> 重置
                </el-button>
                <el-button
                        type="success"
                        plain
                        @click="handleExport"
                        :loading="exportLoading"
                        v-hasPermi="['vos:secret-recharge-record:export']"
                >
                    <Icon icon="ep:download" class="mr-5px" /> 导出
                </el-button>
            </el-form-item>
        </el-form>
    </ContentWrap>

  <!-- 列表 -->
    <ContentWrap>
        <el-table
                row-key="id"
                v-loading="loading"
                :data="list"
                :stripe="true"
                :show-overflow-tooltip="true"
        >
            <el-table-column label="ID" align="center" prop="id" />
            <el-table-column label="租户ID" align="center" prop="rechargeTenantId" />
            <el-table-column label="对应的密钥ID" align="center" prop="secretId" />
            <el-table-column label="充值金额(元)" align="center" prop="rechargeAmount" />
            <el-table-column label="充值前余额(元)" align="center" prop="beforeBalance" />
            <el-table-column label="充值后余额(元)" align="center" prop="afterBalance" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column
                    label="创建时间"
                    align="center"
                    prop="createTime"
                    :formatter="dateFormatter"
                    width="180px"
            />
        </el-table>

        <!-- 分页 -->
        <Pagination
                :total="total"
                v-model:page="queryParams.pageNo"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />
    </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SecretRechargeRecordApi, type SecretRechargeRecord } from '@/api/vos/secretrechargerecord'

/** 密钥充值记录 列表 */
defineOptions({ name: 'SecretRechargeRecord' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<SecretRechargeRecord[]>([]) // 列表的数据
const total = ref(0) // 列表的总条数
const queryParams = reactive<any>({
    pageNo: 1,
    pageSize: 10,
    rechargeTenantId: undefined,
    secretId: undefined,
    rechargeAmount: undefined,
    beforeBalance: undefined,
    afterBalance: undefined,
    remark: undefined,
    createTime: [] // [开始, 结束]
})
const queryFormRef = ref() // 搜索表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
    loading.value = true
    try {
        const data = await SecretRechargeRecordApi.getSecretRechargeRecordPage(queryParams)
        list.value = data.list || []
        total.value = data.total || 0
    } finally {
        loading.value = false
    }
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields?.()
    handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
    try {
        await message.exportConfirm()
        exportLoading.value = true
        const data = await SecretRechargeRecordApi.exportSecretRechargeRecord(queryParams)
        download.excel(data, '密钥充值记录.xls')
    } finally {
        exportLoading.value = false
    }
}

/** 初始化 **/
onMounted(() => {
    getList()
})
</script>
